package org.oiue.service.consume.impl;

import java.util.Map;

import org.oiue.service.bytes.api.BytesService;
import org.oiue.service.cache.CacheServiceManager;
import org.oiue.service.log.LogService;
import org.oiue.service.message.MessageService;
import org.oiue.service.online.OfflineHandler;
import org.oiue.service.online.OnlineHandler;
import org.oiue.service.online.OnlineService;
import org.oiue.service.osgi.FrameActivator;
import org.oiue.service.osgi.MulitServiceTrackerCustomizer;

public class Activator extends FrameActivator {
	
	@Override
	public void start() {
		this.start(new MulitServiceTrackerCustomizer() {
			private ConsumeServiceImpl messageService;
			private OnlineService onlineService;
			
			@Override
			public void removedService() {
				onlineService.unRegisterOnlineHandler("messageService");
				onlineService.unRegisterOfflineHandler("messageService");
			}
			
			@Override
			public void addingService() {
				LogService logService = getService(LogService.class);
				CacheServiceManager cache = getService(CacheServiceManager.class);
				BytesService bytesService = getService(BytesService.class);
				onlineService = getService(OnlineService.class);
				
				messageService = new ConsumeServiceImpl(logService, cache, onlineService, bytesService);
				registerService(MessageService.class, messageService);
				
				onlineService.registerOnlineHandler("messageService", (OnlineHandler) messageService, 1);
				onlineService.registerOfflineHandler("messageService", (OfflineHandler) messageService, 1);
			}
			
			@Override
			public void updatedConf(Map<String, ?> props) {
				messageService.updated(props);
			}
		}, LogService.class, CacheServiceManager.class, OnlineService.class, BytesService.class);
	}
	
	@Override
	public void stop() {}
}
